package cn.edu.cqut.test0514;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.Charset;

import javax.swing.JPanel;

/*
 * 要求：往一个文件中存入一个int数据，一个float类型的数据，一个double类型的数据，
 * 一个字符串；然后通过程序读取出这些数据，显示到屏幕上。
 */
public class Test5 extends JPanel {
	public static void main(String[] args) throws Exception{
		//对于同一个文件的输出和输入规则约束--协议
		File file = new File("test.dat");
		DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(file));
		outputStream.writeInt(10000);
		outputStream.writeFloat(3.14f);
		outputStream.writeDouble(3.14);
//		outputStream.writeBytes(new String("hello word, 你好".getBytes(),"utf-8"));
		outputStream.writeUTF("hello word, 你好");
		outputStream.flush();
		outputStream.close();
		DataInputStream inputStream = new DataInputStream(new FileInputStream(file));
		int a = inputStream.readInt();
		float f = inputStream.readFloat();
		double d = inputStream.readDouble();
//		byte[] data = new byte[1024];
//		int len = inputStream.read(data,0,data.length);
		String str = inputStream.readUTF();
		inputStream.close();
//		String str = new String(data, 0, len, "utf-8");
		System.out.println(a);
		System.out.println(f);
		System.out.println(d);
		System.out.println(str);
		
	}
}
